home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
198_02
/
random.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-01-21
|
30KB
|
1,103 lines
/*
* This file contains the command processing functions for a number of random
* commands. There is no functional grouping here, for sure.
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
/*
* Set fill column to n.
*/
setfillcol(f, n)
{
fillcol = n;
mlwrite("[Fill column is %d]",n);
return(TRUE);
}
/*
* Display the current position of the cursor, in origin 1 X-Y coordinates,
* the character that is under the cursor (in hex), and the fraction of the
* text that is before the cursor. The displayed column is not the current
* column, but the column that would be used on an infinite width display.
* Normally this is bound to "C-X =".
*/
showcpos(f, n)
{
register LINE *lp; /* current line */
register long numchars; /* # of chars in file */
register int numlines; /* # of lines in file */
register long predchars; /* # chars preceding point */
register int predlines; /* # lines preceding point */
register int curchar; /* character under cursor */
int ratio;
int col;
int savepos; /* temp save for current offset */
int ecol; /* column pos/end of current line */
/* starting at the beginning of the buffer */
lp = lforw(curbp->b_linep);
/* start counting chars and lines */
numchars = 0;
numlines = 0;
while (lp != curbp->b_linep) {
/* if we are on the current line, record it */
if (lp == curwp->w_dotp) {
predlines = numlines;
predchars = numchars + curwp->w_doto;
if ((curwp->w_doto) == llength(lp))
curchar = '\n';
else
curchar = lgetc(lp, curwp->w_doto);
}
/* on to the next line */
++numlines;
numchars += llength(lp) + 1;
lp = lforw(lp);
}
/* if at end of file, record it */
if (curwp->w_dotp == curbp->b_linep) {
predlines = numlines;
predchars = numchars;
curchar = '\n';
}
/* Get real column and end-of-line column. */
col = getccol(FALSE);
savepos = curwp->w_doto;
curwp->w_doto = llength(curwp->w_dotp);
ecol = getccol(FALSE);
curwp->w_doto = savepos;
ratio = 0; /* Ratio before dot. */
if (numchars != 0)
ratio = (100L*predchars) / numchars;
/* summarize and report the info */
mlwrite("Line %d/%d Col %d/%d Char %D/%D (%d%%) char = 0x%x (%d)",
predlines+1, numlines+1, col, ecol,
predchars, numchars, ratio, curchar, curchar);
return (TRUE);
}
getcline() /* get the current line number */
{
register LINE *lp; /* current line */
register int numlines; /* # of lines before point */
/* starting at the beginning of the buffer */
lp = lforw(curbp->b_linep);
/* start counting lines */
numlines = 0;
while (lp != curbp->b_linep) {
/* if we are on the current line, record it */
if (lp == curwp->w_dotp)
break;
++numlines;
lp = lforw(lp);
}
/* and return the resulting count */
return(numlines + 1);
}
/*
* Return current column. Stop at first non-blank given TRUE argument.
*/
getccol(bflg)
int bflg;
{
register int c, i, col;
col = 0;
for (i=0; i<curwp->w_doto; ++i) {
c = lgetc(curwp->w_dotp, i);
if (c!=' ' && c!='\t' && bflg)
break;
if (c == '\t')
col += tabsize - (col % tabsize) - 1;
else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
++col;
++col;
}
return(col);
}
/*
* Set current column.
*/
setccol(pos)
int pos; /* position to set cursor */
{
register int c; /* character being scanned */
register int i; /* index into current line */
register int col; /* current cursor column */
register int llen; /* length of line in bytes */
col = 0;
llen = llength(curwp->w_dotp);
/* scan the line until we are at or past the target column */
for (i = 0; i < llen; ++i) {
/* upon reaching the target, drop out */
if (col >= pos)
break;
/* advance one character */
c = lgetc(curwp->w_dotp, i);
if (c == '\t')
col += tabsize - (col % tabsize) - 1;
else if ((c&0x7F) < 0x20 || (c&0x7F) == 0x7F)
++col;
++col;
}
/* set us at the new position */
curwp->w_doto = i;
/* and tell weather we made it */
return(col >= pos);
}
/*
* Twiddle the two characters on either side of dot. If dot is at the end of
* the line twiddle the two characters before it. Return with an error if dot
* is at the beginning of line; it seems to be a bit pointless to make this
* work. This fixes up a very common typo with a single stroke. Normally bound
* to "C-T". This always works within a line, so "WFEDIT" is good enough.
*/
twiddle(f, n)
{
register LINE *dotp;
register int doto;
register int cl;
register int cr;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
dotp = curwp->w_dotp;
doto = curwp->w_doto;
if (doto==llength(dotp) && --doto<0)
return (FALSE);
cr = lgetc(dotp, doto);
if (--doto < 0)
return (FALSE);
cl = lgetc(dotp, doto);
lputc(dotp, doto+0, cr);
lputc(dotp, doto+1, cl);
lchange(WFEDIT);
return (TRUE);
}
/*
* Quote the next character, and insert it into the buffer. All the characters
* are taken literally, with the exception of the newline, which always has
* its line splitting meaning. The character is always read, even if it is
* inserted 0 times, for regularity. Bound to "C-Q"
*/
quote(f, n)
{
register int s;
register int c;
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
c = tgetc();
if (n < 0)
return (FALSE);
if (n == 0)
return (TRUE);
if (c == '\n') {
do {
s = lnewline();
} while (s==TRUE && --n);
return (s);
}
return (linsert(n, c));
}
/*
* Set tab size if given non-default argument (n <> 1). Otherwise, insert a
* tab into file. If given argument, n, of zero, change to true tabs.
* If n > 1, simulate tab stop every n-characters using spaces. This has to be
* done in this slightly funny way because the tab (in ASCII) has been turned
* into "C-I" (in 10 bit code) already. Bound to "C-I".
*/
tab(f, n)
{
if (n < 0)
return (FALSE);
if (n == 0 || n > 1) {
stabsize = n;
return(TRUE);
}
if (! stabsize)
return(linsert(1, '\t'));
return(linsert(stabsize - (getccol(FALSE) % stabsize), ' '));
}
#if AEDIT
detab(f, n) /* change tabs to spaces */
int f,n; /* default flag and numeric repeat count */
{
register int inc; /* increment to next line [sgn(n)] */
if (curbp->b_mode&MDVIEW) /* don't allow this command if */
return(rdonly()); /* we are in read only mode */
if (f == FALSE)
n = 1;
/* loop thru detabbing n lines */
inc = ((n > 0) ? 1 : -1);
while (n) {
curwp->w_doto = 0; /* start at the beginning */
/* detab the entire current line */
while (curwp->w_doto < llength(curwp->w_dotp)) {
/* if we have a tab */
if (lgetc(curwp->w_dotp, curwp->w_doto) == '\t') {
ldelete(1L, FALSE);
insspace(TRUE,
tabsize - (curwp->w_doto % tabsize));
}
forwchar(FALSE, 1);
}
/* advance/or back to the next line */
forwline(TRUE, inc);
n -= inc;
}
curwp->w_doto = 0; /* to the begining of the line */
thisflag &= ~CFCPCN; /* flag that this resets the goal column */
lchange(WFEDIT); /* yes, we have made at least an edit */
retur